﻿namespace Stopwatch
{
    using System;
    using System.IO;
    using System.IO.IsolatedStorage;

    public static class Logger
    {
        static string logfile = "log.log";
        static string errorfile = "error.log";

        static Logger()
        {
        }

        static IsolatedStorageFile storage { get { return IsolatedStorageFile.GetUserStoreForApplication(); } }

        public static void Log(string msg)
        {
#if DEBUG
            lock (logfile)
            {
                Append(logfile, msg);
            }
#endif
        }

        public static void Error(Exception ex)
        {
#if DEBUG
            lock (errorfile)
            {
                Append(errorfile, ex.ToString());
            }
#endif
        }

        static void Append(string path, string text)
        {
            using (var fs = storage.OpenFile(path, System.IO.FileMode.Append))
            using (StreamWriter sw = new StreamWriter(fs))
            {
                sw.WriteLine(DateTime.Now);
                sw.WriteLine(text);
                sw.Flush();
            }
        }
    }
}
